package com.roc.util.constant;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

/**
*@auhor   zbl
*@date    2016年11月25日 下午3:32:14
*@desc    说明  根据变量id获取变量值
**/
@Component
public class ConfigMerinfoUtil {
	
	/**
	 *获取结算类型
	 * */
	public final static String getClearingType(String key){
		Map<String,String> map=ConfigProperties.getSysDB1("Clearing_Type",true);
		return map.get(key);
	}
	/**
	 *获取终端类型
	 * */
	public final static String getPosType(String key){
		Map<String,String> map=ConfigProperties.getSysDB1("Terminal_Type",true);
		return map.get(key);
	}
	
	/**
	 *获取终端品牌
	 * */
	public final static String getPosBRAND(String key){
		Map<String,String> map=ConfigProperties.getSysDB1("TMNL_BRAND",true);
		return map.get(key);
	}
	/**
	 *获取终端型号
	 * */
	
	public final static String getPosMODEL(String key,String brandno){
		Map<String,String> map=ConfigProperties.getSyspostype("TMNL_MODEL_NO",brandno,false);
		return map.get(key);
	}
	/**
	 *获取终端类型 
	 * */
		
		public final static String getTerminalType(String key){
			Map<String,String> map=ConfigProperties.getSysDB1("Terminal_Type",false);
			return map.get(key);
		}
		
	/**
	 * 获取审核
	 * */
	public final static Map<String,String> getAuditingMap(){
		Map<String,String> map=new LinkedHashMap<String, String>();
		map.put(AuditingResultCodeType.PASS.getCode(),AuditingResultCodeType.PASS.getName());
		map.put(AuditingResultCodeType.REFUSE.getCode(),AuditingResultCodeType.REFUSE.getName());
		//map.put(AuditingResultCodeType.INCOMPLETE_INFO.getCode(),AuditingResultCodeType.INCOMPLETE_INFO.getName());
		return map;
	}
	/**
	 * 获取云商宝审核
	 * */
	public final static Map<String,String> getAppAuditingMap(){
		Map<String,String> map=new LinkedHashMap<String, String>();
		map.put(AuditingResultCodeType.PASS.getCode(),AuditingResultCodeType.PASS.getName());
		map.put(AuditingResultCodeType.REFUSE.getCode(),AuditingResultCodeType.REFUSE.getName());
		map.put(AuditingResultCodeType.AUTH_LIMIT.getCode(),AuditingResultCodeType.AUTH_LIMIT.getName());
		return map;
	}
	/**
	 *获取证件类型
	 * */
	public final static String getCertifType(String key){
		Map<String,String> map= ConfigProperties.getSysDB1("CERTIF_TYPE",true);
		return map.get(key);
	}
	
	/**
	 *获取gb省份code用于进件
	 * */
	public final static String getGBProviceCodeForInsert(String key,boolean isName){
		String result="";
		if(isName){
			return getUnionProviceName(key);
		}
		String tmpNameUN=getUnionProviceName(key).substring(0, 2);
		/*Iterator<?> iter=ConfigProperties.getGBProviceProperties().entrySet().iterator();*/
		Iterator<?> iter=ConfigProperties.gbProviceProperties.entrySet().iterator();
		while(iter.hasNext()){
			Map.Entry<?, ?> entry=(Map.Entry<?, ?>)iter.next();
			String[] strs=String.valueOf(entry.getValue()).split("_");
			String tmpNameGB="";
			if(strs.length>1){
				tmpNameGB=strs[1].substring(0,2);
			}else{
				tmpNameGB=strs[0].substring(0,2);
			}
			if(tmpNameUN.equals(tmpNameGB)){
				/*if(isName)  
					result=String.valueOf(entry.getValue());
				else*/
					result= String.valueOf(entry.getKey());
				break;
			}
		}
		return result;
	}
	/**
	 *获取UN省份名称
	 * */
	public final static String getUNProviceName(String key){
		Map<String,String> map= ConfigProperties.unionProviceProperties ;
		return getNameByCode(map, key);
	}
	
	/**
	 *获取GB省份名称
	 * */
	public final static String getGBProviceName(String key){
		Map<String,String> map= ConfigProperties.gbProviceProperties;
		return getNameByCode(map, key);
	}
	
	/**
	 *获取un城市名称
	 * */
	public final static String getUNCityName(String key){
		Map<String,String> map= ConfigProperties.unionCityProperties;
		return getNameByCode(map, key);
	}
	/**
	 *获取GB城市名称
	 * */
	public final static String getGBCityName(String key){
		Map<String,String> map= ConfigProperties.gbCityProperties;
		return getNameByCode(map, key);
	}
	
	/**
	 *获取UN区县名称
	 * */
	public final static String getUNAreaName(String key){
		Map<String,String> map= ConfigProperties.unionAreaProperties;
		return getNameByCode(map, key);
	}
	/**
	 *获取GB区县名称
	 * */
	public final static String getGBAreaName(String key){
		Map<String,String> map= ConfigProperties.gbAreaProperties;
		return getNameByCode(map, key);
	}
	
	 
	/**
	 *获取银联省份名称
	 * */
	public final static String getUnionProviceName(String key){
		
		Map<String,String> map= ConfigProperties.getUnionProviceProperties();
		return getNameByCode(map, key);
	}
	
	/**
	 *获取银联城市名称
	 * */
	public final static String getUnionCityName(String key){
		Map<String,String> map= ConfigProperties.getUnionCityProperties(null);
		return getNameByCode(map, key);
	}
	
	/**
	 *获取银联地区名称
	 * */
	public final static String getUnionAreaName(String key){
		Map<String,String> map= ConfigProperties.getAreaCodeProperties();
		return getNameByCode(map, key);
	}
	
	/**
	 * 过滤产品信息 t0-t1
	 * */
	public final static Map<String, String> getProductList(){
		Map<String, String> product=ConfigProperties.getSysDB1("PROD_CD",true);
		product.remove("2100");//代付
		product.remove("1121");//直联转账
		product.remove("1191");//云闪付（银联扫码）
		product.remove("1291");//_微信反扫支付T1
		product.remove("1391");//支付宝反扫T1
		product.remove("1491");//微信正扫支付T1
		product.remove("1591");//支付宝正扫T1
		return  product;
	}
	
	/**
	 * 过滤产品信息 t0-t1-银联扫码-支付宝微信
	 * */
	public final static Map<String, String> getProductListNew(){
		Map<String, String> product=ConfigProperties.getSysDB1("PROD_CD",true);
		product.remove("2100");//代付
		product.remove("1121");//直联转账
		product.remove("1291");//微信扫码支付
		product.remove("1491");//微信正扫支付
		product.remove("1591");//支付宝正扫支付
		product.remove("1691");//银联扫码反扫
		return product;
	}
	
	/**
	 * pos 开通功能
	 * @param some  开通功能选项  
	 * @param isManager是否审核人员
	 * */
	public final static String T1_FUNCTION="1000";//t1
	public final static String T0_FUNCTION="1171";//t0
	public final static String ALL_FUNCTION="ALL";//所有
	public final static String YL_ZW_FUNCTION="0001004";//银联微信支付宝扫码业务码
	public final static String UNIONPAY_FUNCTION="1191";//银联扫码
	public final static String ZWPAY_FUNCTION="1391";//支付宝反扫 标识
	public final static String ZPAY_FUNCTION_Z="1591";//支付宝正扫
	public final static String WPAY_FUNCTION_Z="1491";//微信正扫
	public final static String WPAY_FUNCTION_F="1291";//微信反扫
	public final static String T0_2005_FUNCTION="0002005";//t0交易划付手续费
	public final static String T0_2004_FUNCTION="0002004";//t0交易产品手续费
	public final static String T1_1000_FUNCTION="0001000";//t1借记卡手续费
	public final static String T1_1001_FUNCTION="0001001";//t1贷记卡手续费
	public final static String T0_2000_FUNCTION="0002000";//t0借记卡手续费
	public final static String T0_2001_FUNCTION="0002001";//t0贷记卡手续费
	public final static String T0_BIZ_CD="0000001";//T0  功能选择
	public final static String T1_BIZ_CD="0000000";//T1 功能选择
	public final static String DIRECT_SUBIZ_CD_1="0001001";
	public final static String DIRECT_SUBIZ_CD_2="0001002";
	public final static String DIRECT_SUBIZ_CD_3="0001003";
	public final static String DIRECT_SUBIZ_CD_4="0001004";
	public final static String DIRECT_SUBIZ_CD_5="0002004";
	public final static String DIRECT_SUBIZ_CD_6="0002005";
	public final static String DIRECT_FUNCTION_ALL="DIRECT_ALL";//直连
	public final static String DIRECT_FUNCTION_T1="3000";//直连T1 
	public final static String DIRECT_FUNCTION_T0="1271";//直连T0
	public final static Map<String, String> getPosFunction(String some,boolean isManger){
		Map<String, String> sysMap=ConfigProperties.getSysDB1("TRANS_CD",false);
		Map<String, String> product=new LinkedHashMap<String, String>();
		if(some.equals(T1_FUNCTION)){
			
			product.put("1011", sysMap.get("1011"));//消费
			product.put("1001", sysMap.get("1001"));//余额查询
			product.put("2011", sysMap.get("2011"));//消费撤销
			product.put("4011", sysMap.get("4011")); //消费冲正
			if(isManger){
				product.put("1021", sysMap.get("1021"));//预授权
				product.put("1031", sysMap.get("1031"));//预授权完成
				product.put("3011", sysMap.get("3011"));//退货
			}
		}else if(some.equals(T0_FUNCTION)){
			product.put("1171", sysMap.get("1171"));//实时收款
		}else if(some.equals(UNIONPAY_FUNCTION)){
			product.put("1191", sysMap.get("1191"));//银联扫码收款
			product.put("1161", sysMap.get("1161"));//银联扫码收款
			product.put("1691", sysMap.get("1691"));//银联反扫扫码收款
			product.put("1661", sysMap.get("1661"));//银联反扫扫码收款
		}else if(some.equals(ZWPAY_FUNCTION)){
			product.put("1391", sysMap.get("1391"));//支付宝扫码支付请求  
			product.put("1291", sysMap.get("1291"));//支付宝正扫支付
			product.put("1491", sysMap.get("1491"));//微信正扫支付
			product.put("1591", sysMap.get("1591"));//微信扫码支付
			product.put("1361", sysMap.get("1361"));//支付宝扫码支付请求  
			product.put("1261", sysMap.get("1261"));//支付宝正扫支付
			product.put("1461", sysMap.get("1461"));//微信正扫支付
			product.put("1561", sysMap.get("1561"));//微信扫码支付
		}
		else{
			product.put("1011", sysMap.get("1011"));//消费
			product.put("1001", sysMap.get("1001"));//余额查询
			product.put("2011", sysMap.get("2011"));//消费撤销
			product.put("4011", sysMap.get("4011")); //消费冲正
			if(isManger){
				product.put("1021", sysMap.get("1021"));//预授权
				product.put("1031", sysMap.get("1031"));//预授权完成
				product.put("3011", sysMap.get("3011"));//退货
			}
			product.put("1171", sysMap.get("1171"));//实时收款
		}
		 
		return product;
	}
	
	/**
	 * 获取手续费
	 * @param code 编码
	 * */
	public final static String getFeeList(String code){
		Map<String, String> fee=ConfigProperties.getSysDB1("SUB_BIZ_CD",false);
		 
		return  fee.get(code);
	}
	
	/**
	 * 根据行业名称 获取mcc 模糊匹配
	 * */
	public static  Map<String,String> getMccByName(String name){
		Map<String, String> result=ConfigProperties.getHyMccTypeProperties();
		Map<String,String> newResult=new TreeMap<String,String>();
		if(null != result && result.size()>0){
			Iterator<?> itor=result.entrySet().iterator();
			while (itor.hasNext()) {
				Map.Entry<?, ?> entry=(Map.Entry<?, ?>)itor.next();
				String value=String.valueOf(entry.getValue());
				if(value.indexOf(name)>-1){//匹配到
					String key=String.valueOf(entry.getKey());
					newResult.put(key, value);
				}
			}
		}
		return newResult;
	}
	
	/**
	 * 根据KEY 获取mcc 精确查找
	 * */
	public static  String  getMccByKey(String key){
		String name="";
		Map<String, String> result=ConfigProperties.getHyMccTypeProperties();
		name=result.get(key);
		return name;
	}
	//公共
	private final static String getNameByCode(Map<String,String> map,String key){
		if( null != map.get(key) ){
			String[] strs=map.get(key).split("_");
			if(strs.length>1){
				return strs[1];
			}else{
				return strs[0];
			}
		}else{
			return null;
		}
	}
	
	//校验商户简称-只能输入汉字、字母、数字且 8~20位
	public static boolean isCheckMerName(String merName){
		String regextStr = "^[a-zA-Z0-9\u4e00-\u9fa5]{8,20}$";
		Pattern pattern = Pattern.compile(regextStr);
		return pattern.matcher(merName).matches();
	}
	
	public static boolean isCheckValidDoc(String value){
		String regextStr = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
		Pattern pattern = Pattern.compile(regextStr);
		return pattern.matcher(value).matches();
	}
	
	public final static Map<String, String> getDirectProductTypeList(){
		Map<String, String> dproduct = new HashMap<String, String>();
		dproduct.put("3000", "直连T+1");
		dproduct.put("1271", "直连T+0");
		return dproduct;
	}
	
	public final static Map<String, String> getDirectProductList(String some, boolean isAll){
		Map<String, String> ditproduct = new HashMap<String, String>();
		if(DIRECT_FUNCTION_T0.equals(some)){
			ditproduct.put("1271", "直连T+0实时收款");
		}else if(DIRECT_FUNCTION_T1.equals(some)){
			ditproduct.put("1111", "直连消费");
			ditproduct.put("1601", "直连余额查询");
			ditproduct.put("2111", "直连消费撤销");
			ditproduct.put("4111", "直连消费冲正");
		}else {
			ditproduct.put("1111", "直连消费");
			ditproduct.put("1601", "直连余额查询");
			ditproduct.put("2111", "直连消费撤销");
			ditproduct.put("4111", "直连消费冲正");
			ditproduct.put("1271", "直连T+0实时收款");
			if(isAll){
				ditproduct.put("1231", "直连预授权完成");
				ditproduct.put("1321", "直连预授权");
				ditproduct.put("2121", "直连预授权撤销");
				ditproduct.put("2141", "直连预授权完成撤销");
			}
		}
		return ditproduct;
	}
}
